#!/usr/bin/env python
# -*- coding: utf8 -*-
# title       :
# description :
# author      :'ShenMeng'

import os
from PySide import QtGui, QtCore
from pyuic.asset_create_widget import Ui_asset_create_widget
from search_combo_box import SearchComboBox
from library_style import STANDARD_STYLE
from pyuic import resources_rc

class AssetCreator(QtGui.QDialog):
    def __init__(self, parent=None):
        super(AssetCreator, self).__init__(parent)
        self.ui = Ui_asset_create_widget()
        self.ui.setupUi(self)
        self.setWindowTitle(u'资产目录生成器')
        self.setMinimumSize(400, 200)
        self.setStyleSheet(STANDARD_STYLE + """
            QAbstractItemView
            {
                border: 1px solid rgb(0, 120, 215);
                outline:0
                px;
                background:rgb(35, 35, 35);
                color:white;
            }
            QPushButton{background:rgb(100,100,100);}""")
        self.ui.type_open_btn.setIcon(QtGui.QIcon(":/icons/win_folder.png"))
        self.ui.type_open_btn.setEnabled(0)
        self.ui.asset_open_btn.setIcon(QtGui.QIcon(":/icons/win_folder.png"))
        self.ui.asset_open_btn.setEnabled(0)
        self.asset_type_text = SearchComboBox()
        self.ui.type_layout.addWidget(self.asset_type_text)

        self.ui.browser_btn.clicked.connect(self.on_browser_btn_clicked)
        self.asset_type_text.editTextChanged.connect(self.on_asset_type_text_changed)
        self.ui.asset_name_text.textChanged.connect(self.on_asset_name_text_changed)
        self.ui.type_open_btn.clicked.connect(self.on_type_open_btn_clicked)
        self.ui.asset_open_btn.clicked.connect(self.on_asset_open_btn_clicked)
        self.ui.cancle_btn.clicked.connect(self.close)
        self.ui.ok_btn.clicked.connect(self.on_ok_btn_clicked)


    def on_ok_btn_clicked(self):
        pass


    def on_type_open_btn_clicked(self):
        root_path = self.ui.asset_path_text.text()
        type_name = self.asset_type_text.currentText()
        file_path = os.path.join(root_path, type_name)
        if os.path.isdir(file_path):
            os.startfile(file_path)

    def on_asset_open_btn_clicked(self):
        root_path = self.ui.asset_path_text.text()
        type_name = self.asset_type_text.currentText()
        asset_name = self.ui.asset_name_text.text()
        file_path = os.path.join(root_path, type_name, asset_name)
        if os.path.isdir(file_path):
            os.startfile(file_path)

    def on_browser_btn_clicked(self):
        path = self.ui.asset_path_text.text()
        result_path = QtGui.QFileDialog.getExistingDirectory(self, dir=path)
        self.ui.asset_path_text.setText(result_path)
        self.set_data(result_path)

    def set_data(self, path):
        if not os.path.isdir(path):
            self.asset_type_text.clear_data()
            self.ui.type_open_btn.setEnabled(0)
            return
        file_list = os.listdir(path)
        result_list = []
        for f in file_list:
            if os.path.isdir(os.path.join(path, f)):
                result_list.append(f)
        self.asset_type_text.set_data(result_list)
        self.asset_type_text.setCurrentIndex(-1)
        self.ui.type_open_btn.setEnabled(0)

    def on_asset_type_text_changed(self, text):
        if not text:
            self.ui.asset_type_info.setText('')
        else:
            if self.asset_type_text.findText(text, QtCore.Qt.MatchExactly) == -1:
                self.ui.asset_type_info.setStyleSheet('color:orange;')
                self.ui.asset_type_info.setText(u'分类不存在，将创建该分类目录！')
                self.ui.type_open_btn.setEnabled(0)
            else:
                self.ui.asset_type_info.setStyleSheet('color:rgb(0, 120, 210);')
                self.ui.asset_type_info.setText(u'分类已存在，将添加到该分类目录！')
                self.ui.type_open_btn.setEnabled(1)
        self.on_asset_name_text_changed()

    def on_asset_name_text_changed(self):
        if self._check_file_exists():
            self.ui.asset_name_text.setStyleSheet('color:rgb(200,200,200);')
        else:
            self.ui.asset_name_text.setStyleSheet('color:red;')

    def _check_file_exists(self):
        asset_path = self.ui.asset_path_text.text()
        type_name = self.asset_type_text.currentText()
        asset_name = self.ui.asset_name_text.text()
        if type_name and asset_name and asset_path:
            file_path = os.path.join(asset_path, type_name, asset_name).replace('\\','/')
            print file_path
            if os.path.isdir(file_path):
                self.ui.asset_name_info.setStyleSheet('color:red;')
                self.ui.asset_name_info.setText(u'该资产已存在，请重新输入！')
                self.ui.asset_name_text.setStyleSheet('color:red;')
                self.ui.asset_open_btn.setEnabled(1)
                return
            else:
                self.ui.asset_name_info.setStyleSheet('color:rgb(0, 120, 210);')
                self.ui.asset_name_info.setText(u'该资产不存在，将创建该资产！')
                self.ui.asset_name_text.setStyleSheet('color:rgb(200,200,200);')
                self.ui.asset_open_btn.setEnabled(0)
                return file_path
        else:
            self.ui.asset_name_info.setText('')
            self.ui.asset_open_btn.setEnabled(0)

    def closeEvent(self, event):
        self.asset_type_text._set_view_hide()
        super(AssetCreator, self).closeEvent(event)

    def moveEvent(self, event):
        self.asset_type_text._set_view_hide()
        super(AssetCreator, self).moveEvent(event)

    def mousePressEvent(self, event):
        self.asset_type_text._set_view_hide()
        super(AssetCreator, self).mousePressEvent(event)
if __name__ == '__main__':
    app = QtGui.QApplication([])
    ac = AssetCreator()
    ac.show()
    app.exec_()